package net.zjitc.ktv;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author admin
 * @Date 2019/4/3 8:19
 */
public class SongDaoimp implements SongDao {

    ArrayList<Song> arrayList = new ArrayList<Song>();

    @Override
    public void init() {
        Song song1 = new Song("佛说");
        Song song2 = new Song("十年");
        Song song3 = new Song("夜上海");
        Song song4 = new Song("勋章");
        Song song5 = new Song("空城");
        arrayList.add(song1);
        arrayList.add(song2);
        arrayList.add(song3);
        arrayList.add(song4);
        arrayList.add(song5);
    }

    @Override
    public void add(Song song) {
        arrayList.add(song);
        System.out.println("已添加歌曲" + song.getName());
    }

    @Override
    public void all() {
        System.out.println("当前歌曲列表:");

    }

    @Override
    public void top(String name) {
        Song song = select(name);
        if (song == null) {
            System.out.println("当前列表没有此歌曲");
        } else {
            arrayList.remove(song);
            arrayList.add(0, song);
            System.out.println("已将歌曲" + song.getName() + "置顶");
        }
    }

    @Override
    public Song select(String name) {
        for (int i = 0; i < arrayList.size(); i++) {
            if (arrayList.get(i).getName().equals(name)) {
                return arrayList.get(i);
            }
        }
        return null;
    }

    @Override
    public void before(Song music) {
        int index = arrayList.indexOf(music);
        arrayList.remove(music);
        arrayList.add(index - 1, music);
    }
}
